// 组件异步加载器
const loadPage = function(page:any) {
    const tmpArr = page.split('/'),
        path = tmpArr.slice(0, -1).join('/'),
        com = tmpArr[tmpArr.length - 1];
    return () => import(`@/pages${path}/${com}.vue`);
};

// 生成可访问路由
export function generateRoutes(data:any) {
    // console.log('11,,,', origin_data, data);
    // data.forEach(item => {
    //     origin_data.forEach(ite => {
    //         if(item)
    //     })
    // })
    let tmpArr = [];
    // 先取出顶级菜单
    function mixinMenu(data:any) {
        interface Types {
            path: string;
            name: string;
            component: () => void;
            meta: object;
        }
        const arr: any[] = [];
        data = data.filter((item:any, index:any) => {
            if(!item.pid) {
                if(item.type == 0 || item.type == 1) {
                    const tmpObj:Types = {
                        path: item.path,
                        name: item.name,
                        component: loadPage(item.component),
                        meta: { id: item._id, index: item._id.slice(0, 11), title: item.title, icon: item.icon, permi: item.permi, type: item.type, show: item.show, date: item.date, sort: item.sort, pid: item.pid }
                    }
                    arr.push(tmpObj);
                }
            }
            return item.pid != '';
        });
        interface obj2 {
            pid: string;
            type: number;
            path: string;
            name: string;
            component: () => void;
            _id: string;
            title: string;
            icon: string;
            permi: string;
            show: boolean;
            date: Date;
            sort: number;
        }
        function mixinChild(data:any, arr:any) {
            data.forEach((item:obj2, index:number) => {
                function ploy(arr:any) {
                    arr.forEach((ite:any) => {
                        if(item.pid === ite.meta.id) {
                            if(item.type == 0 || item.type == 1) {
                                const rMode = {path: item.path, name: item.name, component: loadPage(item.component), meta: { id: item._id, index: item._id.slice(0, 11), title: item.title, icon: item.icon, permi: item.permi, type: item.type, show: item.show, date: item.date, sort: item.sort, pid: item.pid }}
                                ite.children ? ite.children.push(rMode) : ite['children'] = [rMode];
                                data.splice(index, 1, null);
                            }
                        }else {
                            data.splice(index, 1, null)
                            if(ite.children) {
                                ploy(ite.children);
                            }
                        }
                    })
                }
                ploy(arr);
            })
            data = data.filter((item:obj2) => { return item != null; });
            if(data.length > 0) {
                mixinChild(data, arr);
            }
        }
        mixinChild(data, arr);
        return arr;
    }
    tmpArr = mixinMenu(data);
    return tmpArr;
}
// 生成菜单树
export function generateMenus(data:any) {
    let tmpArr = [];
    // 先取出顶级菜单
    function mixinMenu(data:any) {
        const arr: any[] = [];
        data = data.filter((item:any, index:number) => {
            if(!item.pid) {
                arr.push({ path: item.path, name: item.name, component: loadPage(item.component), meta: { id: item._id, index: item._id.slice(0, 11), title: item.title, icon: item.icon, permi: item.permi, type: item.type, show: item.show, date: item.date, sort: item.sort, pid: item.pid } });
            }
            return item.pid != '';
        });
        function mixinChild(data:any, arr:any) {
            data.forEach((item:any, index:number) => {
                function ploy(arr:any) {
                    arr.forEach((ite:any) => {
                        if(item.pid === ite.meta.id) {
                            const rMode = {path: item.path, name: item.name, component: loadPage(item.component), meta: { id: item._id, index: item._id.slice(0, 11), title: item.title, icon: item.icon, permi: item.permi, type: item.type, show: item.show, date: item.date, sort: item.sort, pid: item.pid }}
                            ite.children ? ite.children.push(rMode) : ite['children'] = [rMode];
                            data.splice(index, 1, null);
                        }else {
                            if(ite.children) {
                                ploy(ite.children);
                            }
                        }
                    })
                }
                ploy(arr);
            })
            data = data.filter((item:any) => { return item != null; });
            if(data.length > 0) {
                mixinChild(data, arr);
            }
        }
        mixinChild(data, arr);
        return arr;
    }
    tmpArr = mixinMenu(data);
    
    return tmpArr;
}